"\ndef monthly_metrict_shifted(x,metric = 'std'):\n \n \n # Step 1: Calculate monthly mean price\n monthly_mean = (x.groupby(x.index.to_period('M')).transform('std') )\n # Shift it\n monthly_mean_shifted = monthly_mean.shift(1)\n # Map it back to each day using index.to_period\n xout = x.index.to_period('M').map(monthly_mean_shifted)\n \n return xout\n"
"\nx = dfRet.iloc[:,0]\n\ndef monthly_shifted_std(x,period='M'):\n '''\n x is a pandas.core.series.Series with DatetimeIndex\n \n xout is a pandas.core.frame.DataFrame with same DatetimeIndex and three columns \n \n x = dfRet.iloc[:,0]\n y = dfRet.iloc[:,1]\n\n xycorr = monthly_correl_shifted(x, y)\n '''\n\n\n # Step 1: Calculate monthly metric as dictionary with {month: metric} \n monthly_metric = x.groupby(x.index.to_period('M')).std() \n\n xout = pd.DataFrame(x)\n\n xout['std_monthly'] = x.index.to_period('M').map(monthly_metric)\n\n # Shift it\n monthly_metric_shifted = monthly_metric.shift(1)\n\n # and then map it back\n xout['std_monthly_shifted'] = x.index.to_period('M').map(monthly_metric_shifted)\n\n return xout\n\n"
Download Bond Data¶
| DGS1MO | DGS3MO | DGS1 | DGS2 | DGS5 | DGS10 | DGS30 | |
|---|---|---|---|---|---|---|---|
| DATE | |||||||
| 1962-01-02 | NaN | NaN | 3.22 | NaN | 3.88 | 4.06 | NaN |
| 1962-01-03 | NaN | NaN | 3.24 | NaN | 3.87 | 4.03 | NaN |
| 1962-01-04 | NaN | NaN | 3.24 | NaN | 3.86 | 3.99 | NaN |
| 1962-01-05 | NaN | NaN | 3.26 | NaN | 3.89 | 4.02 | NaN |
| 1962-01-08 | NaN | NaN | 3.31 | NaN | 3.91 | 4.03 | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2025-06-16 | 4.24 | 4.43 | 4.10 | 3.97 | 4.04 | 4.46 | 4.96 |
| 2025-06-17 | 4.23 | 4.42 | 4.10 | 3.94 | 3.99 | 4.39 | 4.88 |
| 2025-06-18 | 4.20 | 4.42 | 4.10 | 3.94 | 3.98 | 4.38 | 4.88 |
| 2025-06-19 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2025-06-20 | 4.20 | 4.39 | 4.07 | 3.90 | 3.96 | 4.38 | 4.89 |
16559 rows × 7 columns
Download Inflation Data¶
Parameters¶
US Bond Annualized Volatility Vs It's Yield Term¶
Mean and Median Annual Vol. of S&P500 in Decadal Periods¶
Mean and Median Annual Vol. of S&P500 in Decadal Periods¶
Calculate Returns¶
Calculate Correlation, Volatility,¶
| corr | vol_bond | vol_stock | |
|---|---|---|---|
| 1970-02-28 | NaN | NaN | NaN |
| 1970-03-31 | NaN | NaN | NaN |
| 1970-04-30 | NaN | NaN | NaN |
| 1970-05-31 | NaN | NaN | NaN |
| 1970-06-30 | NaN | NaN | NaN |
| ... | ... | ... | ... |
| 2025-02-28 | 0.602267 | 0.028993 | 0.046418 |
| 2025-03-31 | 0.602329 | 0.028793 | 0.047201 |
| 2025-04-30 | 0.595878 | 0.028764 | 0.046799 |
| 2025-05-31 | 0.572312 | 0.028823 | 0.047437 |
| 2025-06-30 | 0.570876 | 0.028722 | 0.047388 |
665 rows × 3 columns
Set Weights: Constant Mix 60/40 Portfolio¶
Calculate Risk Premiums and Sharpe of 60/40 Portfolio¶
| premium | corr | |
|---|---|---|
| 1970-02-28 | NaN | NaN |
| 1970-03-31 | NaN | NaN |
| 1970-04-30 | NaN | NaN |
| 1970-05-31 | NaN | NaN |
| 1970-06-30 | NaN | NaN |
| ... | ... | ... |
| 2025-02-28 | 0.068235 | 0.602267 |
| 2025-03-31 | 0.054583 | 0.602329 |
| 2025-04-30 | 0.049591 | 0.595878 |
| 2025-05-31 | 0.056154 | 0.572312 |
| 2025-06-30 | 0.055749 | 0.570876 |
665 rows × 2 columns
Risk Premium¶
Risk Premium of Bonds vs Correlation between Stocks and Bonds¶
Bond and Stock Performance in a given year¶
Performance of Bonds, Stocks and 40/60 Constant Mix During 80-90 and 2000 onwards¶
Explaning the Bond-Stock Correlation¶
| Dep. Variable: | corr | R-squared: | 0.454 |
|---|---|---|---|
| Model: | OLS | Adj. R-squared: | 0.453 |
| Method: | Least Squares | F-statistic: | 359.8 |
| Date: | Tue, 24 Jun 2025 | Prob (F-statistic): | 8.24e-59 |
| Time: | 00:20:45 | Log-Likelihood: | -41.264 |
| No. Observations: | 434 | AIC: | 86.53 |
| Df Residuals: | 432 | BIC: | 94.67 |
| Df Model: | 1 | ||
| Covariance Type: | nonrobust |
| coef | std err | t | P>|t| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| const | -0.3923 | 0.027 | -14.624 | 0.000 | -0.445 | -0.340 |
| DGS10 | 0.0755 | 0.004 | 18.969 | 0.000 | 0.068 | 0.083 |
| Omnibus: | 18.010 | Durbin-Watson: | 0.043 |
|---|---|---|---|
| Prob(Omnibus): | 0.000 | Jarque-Bera (JB): | 19.491 |
| Skew: | 0.510 | Prob(JB): | 5.86e-05 |
| Kurtosis: | 2.803 | Cond. No. | 14.4 |
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
| Dep. Variable: | corr | R-squared: | 0.000 |
|---|---|---|---|
| Model: | OLS | Adj. R-squared: | -0.002 |
| Method: | Least Squares | F-statistic: | 0.05077 |
| Date: | Tue, 24 Jun 2025 | Prob (F-statistic): | 0.822 |
| Time: | 00:20:45 | Log-Likelihood: | -245.63 |
| No. Observations: | 618 | AIC: | 495.3 |
| Df Residuals: | 616 | BIC: | 504.1 |
| Df Model: | 1 | ||
| Covariance Type: | nonrobust |
| coef | std err | t | P>|t| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| const | 0.0567 | 0.015 | 3.911 | 0.000 | 0.028 | 0.085 |
| bond | 0.1043 | 0.463 | 0.225 | 0.822 | -0.805 | 1.013 |
| Omnibus: | 1179.738 | Durbin-Watson: | 0.012 |
|---|---|---|---|
| Prob(Omnibus): | 0.000 | Jarque-Bera (JB): | 46.351 |
| Skew: | -0.150 | Prob(JB): | 8.61e-11 |
| Kurtosis: | 1.693 | Cond. No. | 31.9 |
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Bond-Stock Optimal Portfolio¶
Calculate the minimal-variance portfolio
$$ w_{B} = \frac{\sigma^2_S - \sigma_S \sigma_B \rho}{\sigma^2_S + \sigma^2_B - 2 \sigma_S \sigma_B \rho}\\ w_{S} = 1 - w_{B} $$
Theportfolio of the current month, uses the mean $\rho$, $\sigma_S$ and $\sigma_B$ from the previous month.
| bond | stock | |
|---|---|---|
| 1970-02-28 | NaN | NaN |
| 1970-03-31 | NaN | NaN |
| 1970-04-30 | NaN | NaN |
| 1970-05-31 | NaN | NaN |
| 1970-06-30 | NaN | NaN |
| ... | ... | ... |
| 2025-02-28 | 0.998848 | 0.001152 |
| 2025-03-31 | 0.978115 | 0.021885 |
| 2025-04-30 | 0.992661 | 0.007339 |
| 2025-05-31 | 0.982130 | 0.017870 |
| 2025-06-30 | 0.968169 | 0.031831 |
665 rows × 2 columns
Performance¶
| bond | stock | port6040 | |
|---|---|---|---|
| 1970-02-28 | 0.079199 | 0.051352 | 0.062491 |
| 1970-03-31 | -0.016824 | 0.001451 | -0.005859 |
| 1970-04-30 | -0.068870 | -0.094842 | -0.084453 |
| 1970-05-31 | -0.012050 | -0.062904 | -0.042562 |
| 1970-06-30 | 0.025043 | -0.051328 | -0.020779 |
| ... | ... | ... | ... |
| 2025-02-28 | 0.032564 | -0.014344 | 0.004419 |
| 2025-03-31 | 0.000959 | -0.059267 | -0.035176 |
| 2025-04-30 | 0.005758 | -0.007654 | -0.002289 |
| 2025-05-31 | -0.023013 | 0.059705 | 0.026618 |
| 2025-06-30 | 0.002874 | 0.009453 | 0.006821 |
665 rows × 3 columns
| 60/40 | Din | |
|---|---|---|
| 1980-2000 | 0.489929 | 0.361126 |
| 2000-2020 | 0.133496 | 0.145078 |